#include <iostream>
#include "friend.pb.h"
#include "rpcapplication.h"
#include "logger.h"

/*
FriendService原来是一个本地服务,提供了两个进程内的本地方法Login和GetFriendLists
*/

class FriendService : public fixbug::FriendServiceRpc
{
public:
    std::vector<std::string> GetFriendsList(uint32_t userid)
    {
        std::cout << "doing Friend service: GetFriendsList" << std::endl;
        std::cout << "userid: " << userid << std::endl;
        std::vector<std::string> vec;
        vec.push_back("gao yang");
        vec.push_back("li ming");
        vec.push_back("liu zhuo");
        return vec;
    }

    virtual void GetFriendsList(google::protobuf::RpcController *controller,
                                const ::fixbug::GetFriendsListRequest *request,
                                ::fixbug::GetFriendsListResponse *response,
                                ::google::protobuf::Closure *done)
    {
        uint32_t userid = request->userid();

        std::vector<std::string> vec = GetFriendsList(userid);

        for(std::string& name : vec)
        {
            *(response->add_friends()) = name;
        }

        done->Run();
    }
};

int main(int argc, char **argv)
{
    // FriendService us;
    // us.Login("xxx", "123456");

    // 调用框架的初始化操作
    RpcApplication::Init(argc, argv);

    // provider是一个rpc网络服务对象,把FriendService对象发布到rpc节点上
    RpcProvider provider;
    provider.NotifyService(new FriendService());

    // 启动一个rpc服务发布节点, Run以后,进程进入阻塞状态,等待远程的rpc调用
    provider.Run();

    return 0;
}